package controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import service.BoardService;
import dto.AdminDTO;
import dto.BoardDTO;
import dto.CompanyDTO;
import dto.ListBoardDTO;
import dto.MemberDTO;

@Controller
public class BoardController {
	private BoardService service;

	public BoardController(BoardService service) {
		this.service = service;
	}

	@RequestMapping("boardInsert.do")
	public ModelAndView insert(BoardDTO bdto, String contentGroup,
			HttpSession session, HttpServletRequest request) {
		ModelAndView mv = null;

		MemberDTO mdto = (MemberDTO) session.getAttribute("memberDTO");
		AdminDTO adto = (AdminDTO) session.getAttribute("adminDTO");
		CompanyDTO cdto = (CompanyDTO) session.getAttribute("companyDTO");

		String member = null;
		String admin = null;
		String company = null;

		// 회원 종류 확인
		if (mdto != null) {
			member = mdto.getId();
		}

		if (adto != null) {
			admin = adto.getId();
		}
		if (cdto != null) {
			company = cdto.getId();
		}

		if (member != null) {
			bdto.setBoardMemberId(member);
		} else if (admin != null) {
			bdto.setAdminMemberId(admin);
		} else if (company != null) {
			bdto.setCompanyMemberId(company);
		}

		try {
			int page = 0;

			if (request.getParameter("page") == null
					|| request.getParameter("page").equals("")) {
				page = 1;
			} else {
				page = Integer.parseInt(request.getParameter("page"));
			}
			if (bdto.getContent() != null) {
				service.insertBoard(bdto);
				ListBoardDTO listBoardDTO = service.paging(page, contentGroup);
				mv = new ModelAndView("board_insert_tiles.do", "boardAllList",
						listBoardDTO);

			} else if (bdto.getContent() == null) {
				ListBoardDTO listBoardDTO = service.paging(page, contentGroup);
				mv = new ModelAndView("board_insert_tiles.do", "boardAllList",
						listBoardDTO);
			} else {
				mv = new ModelAndView("error.jsp", "error_message", "글 작성 오류");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}

	@RequestMapping("boardContent.do")
	public ModelAndView boardContent(int no, HttpServletRequest request) {
		request.setAttribute("page", request.getParameter("page"));
		return new ModelAndView("board_content_tiles.do", "show_content",
				service.boardContent(no));
	}

	@RequestMapping("boardContentNoti.do")
	public ModelAndView boardContentNoti(int no) {
		return new ModelAndView("board_content_noti_tiles.do", "show_content",
				service.boardContent(no));
	}

	@RequestMapping("boardNoti.do")
	public ModelAndView insert_noti(BoardDTO bdto, int page, HttpSession session) {
		ModelAndView mv = null;
		try {
			AdminDTO adto = (AdminDTO) session.getAttribute("adminDTO");
			String admin = null;

			if (adto != null) {
				admin = adto.getId();
				service.insertBoard(bdto);
				mv = new ModelAndView("board_noti_tiles.do", "paging",
						service.paging(page, "1"));
			} else {
				mv = new ModelAndView("board_noti_tiles.do", "paging",
						service.paging(page, "1"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}

	@RequestMapping("showNoti.do")
	public ModelAndView show_noti() {
		ArrayList list = null;

		try {
			list = (ArrayList) service.selectAllBoardNoti();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView("board_noti_tiles.do", "boardAllListNoti", list);
	}

	@RequestMapping("boardDelete.do")
	public ModelAndView deleteBoard(int no, String contentGroup,
			HttpServletRequest request) {
		ModelAndView mv = null;
		request.setAttribute("page", request.getParameter("page"));
		service.deleteBoard(no);
		mv = new ModelAndView("selectBoardByPage.do");
		return mv;
	}

	@RequestMapping("boardModify.do")
	public ModelAndView modifyBoard(int no, HttpServletRequest request) {
		request.setAttribute("page", request.getParameter("page"));
		return new ModelAndView("board_modify_tiles.do", "show_content",
				service.boardContent(no));
	}

	@RequestMapping("boardUpdate.do")
	public ModelAndView updateBoard(BoardDTO bdto) {
		ModelAndView mv = null;
		try {

			service.updateBoard(bdto);

			bdto = service.boardContent(bdto.getNo());

			if (!bdto.getContentGroup().equals("1")) {
				mv = new ModelAndView("board_content_tiles.do", "show_content",
						bdto);
			} else {
				mv = new ModelAndView("board_content_noti_tiles.do",
						"show_content", bdto);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mv;
	}

	@RequestMapping("boardReply.do")
	public ModelAndView replyBoard(BoardDTO bdto, HttpSession session) {
		ModelAndView mv = null;

		MemberDTO mdto = (MemberDTO) session.getAttribute("memberDTO");
		AdminDTO adto = (AdminDTO) session.getAttribute("adminDTO");
		CompanyDTO cdto = (CompanyDTO) session.getAttribute("companyDTO");

		String member = null;
		String admin = null;
		String company = null;

		if (mdto != null) {
			member = mdto.getId();
		}

		if (adto != null) {
			admin = adto.getId();
		}
		if (cdto != null) {
			company = cdto.getId();
		}

		if (member != null) {
			bdto.setBoardMemberId(member);
		} else if (admin != null) {
			bdto.setAdminMemberId(admin);
		} else if (company != null) {
			bdto.setCompanyMemberId(company);
		}

		service.replyBoard(bdto);

		if (!bdto.getContentGroup().equals("1")) {
			mv = new ModelAndView("board_content_tiles.do", "show_content",
					service.boardContent(bdto.getNo()));
		} else {
			mv = new ModelAndView("board_content_noti_tiles.do",
					"show_content", service.boardContent(bdto.getNo()));
		}
		return mv;
	}

	@RequestMapping("boardReplyForm.do")
	public ModelAndView replyBoradForm(int no) {
		BoardDTO bdto = service.boardContent(no);
		return new ModelAndView("board_reply_tiles.do", "show_content", bdto);
	}

	@RequestMapping("deleteFile.do")
	public ModelAndView deleteFile(int no) {

		ModelAndView mv = null;
		BoardDTO bdto = null;
		try {
			bdto = service.boardContent(no);
			service.deleteFile(bdto);
			mv = new ModelAndView("board_modify_tiles.do", "show_content", bdto);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("/error.do", "error_message", e.getMessage());
		}
		return mv;
	}

	@RequestMapping("selectBoardByPage.do")
	public ModelAndView paging(HttpServletRequest request) {
		ModelAndView mv = null;
		ListBoardDTO listBoardDTO = null;
		String contentGroup = request.getParameter("contentGroup");
		try {
			int page = 0;
			if (request.getParameter("page") == null
					|| request.getParameter("page").equals("")) {
				page = 1;
			} else {
				page = Integer.parseInt(request.getParameter("page"));
			}

			listBoardDTO = service.paging(page, contentGroup);
			if (contentGroup.equals("1")) {
				mv = new ModelAndView("board_noti_tiles.do", "paging",
						listBoardDTO);
			} else {
				mv = new ModelAndView("boardInsert.do", "paging", listBoardDTO);
			}
			request.setAttribute("contentGroup", contentGroup);
			request.setAttribute("page", page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("searchByPaging.do")
	public ModelAndView searchByPaging(int page, BoardDTO bdto){
		ModelAndView mv = null;
		
		ListBoardDTO listDTO = service.pagingSearch(page, bdto);
		
		return mv;
	}
}